<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('after_sales_requests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade')->comment('订单ID');
            $table->foreignId('user_id')->constrained()->onDelete('cascade')->comment('用户ID');
            
            // 售后类型
            $table->enum('type', ['return', 'refund', 'exchange'])->comment('售后类型：return-退货，refund-退款，exchange-换货');
            
            // 申请信息
            $table->string('reason')->comment('申请原因');
            $table->text('description')->nullable()->comment('详细描述');
            $table->json('images')->nullable()->comment('凭证图片');
            
            // 状态管理
            $table->enum('status', ['pending', 'approved', 'rejected', 'processing', 'completed', 'cancelled'])
                ->default('pending')
                ->comment('状态：pending-待审核，approved-已通过，rejected-已拒绝，processing-处理中，completed-已完成，cancelled-已取消');
            
            // 金额信息
            $table->decimal('requested_amount', 10, 2)->default(0)->comment('申请退款金额');
            $table->decimal('approved_amount', 10, 2)->default(0)->comment('批准退款金额');
            
            // 时间记录
            $table->timestamp('submitted_at')->nullable()->comment('提交时间');
            $table->timestamp('approved_at')->nullable()->comment('审核通过时间');
            $table->timestamp('rejected_at')->nullable()->comment('拒绝时间');
            $table->timestamp('completed_at')->nullable()->comment('完成时间');
            $table->timestamp('cancelled_at')->nullable()->comment('取消时间');
            
            // 处理信息
            $table->text('admin_comment')->nullable()->comment('管理员备注');
            $table->text('rejection_reason')->nullable()->comment('拒绝原因');
            
            $table->timestamps();
            
            // 索引
            $table->index(['user_id', 'status']);
            $table->index(['order_id', 'type']);
            $table->index('status');
            $table->index('submitted_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('after_sales_requests');
    }
};
